package com.cloudsigns.factorymanager2.engineering.model;

import android.text.TextUtils;

import com.cloudsigns.factorymanager2.MyApplicaton;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.bean.MediaBean;
import com.cloudsigns.factorymanager2.engineering.bean.PatrolFeedbackBean;
import com.cloudsigns.factorymanager2.engineering.bean.PatrolOrderBean;
import com.cloudsigns.factorymanager2.engineering.utils.EngineeringUtil;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;
import com.cloudsigns.factorymanager2.project.utils.ProjectUtil;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * [ [巡查]获取巡查大区(机场)灯箱列表-给地图/表格用 ]
 * Created by tony on 2018/5/12.
 */

public class MediaListForPatrolMapForManagerModel implements IMediaListForPatrolMapForManagerModel {


    @Override
    public void getPatrolMediaListForMap(final String regionId, final OnPatrolMediaListForMapListener patrolMediaListForMapListener) {
        new Thread(new Runnable() {
            @Override
            public void run() {

                try {
                    String url = ProjectUtil.MEDIA_LIST_FOR_PATROLMAP_FOR_MANAGER + "?id=" + regionId;
                    HttpClient client = new DefaultHttpClient();
                    HttpGet httpGet = new HttpGet(url);

                    HttpResponse response;

                    httpGet.addHeader("token", MyApplicaton.getApplication().getToken());
                    httpGet.addHeader(HTTP.CONTENT_TYPE, ConstantUtil.CONTENT_TYPE);


                    response = client.execute(httpGet);
                    String result = EntityUtils.toString(response.getEntity(), "UTF-8").trim();
                    JSONObject json = new JSONObject(result);
                    String code = json.getString("code");
                    if (null != patrolMediaListForMapListener) {
                        if (code.equals(EngineeringUtil.HTTP_SUCCESSED_COD)) {
                            JSONArray dataJsonArr = new JSONArray(json.getString("data"));
                            //  if (null == mediaListJsonArrStr || TextUtils.isEmpty(mediaListJsonArrStr) || mediaListJsonArrStr.equals("null")) {
                            if (null != dataJsonArr) {
                                List<MediaBean> mediaBeanList = new ArrayList<MediaBean>();
                                for (int i = 0; i < dataJsonArr.length(); i++) {
                                    JSONObject mediaJsonObj = (JSONObject) dataJsonArr.get(i);

                                    MediaBean mediaBean = new MediaBean();
                                    mediaBean.setAreaName(mediaJsonObj.getString("areaName"));

                                    String handleDate = mediaJsonObj.getString("handleDate");
                                    if (!TextUtils.isEmpty(handleDate) && !handleDate.equals("null")) {
                                        mediaBean.setHandleDate(Long.parseLong(handleDate));
                                    }

                                    mediaBean.setMediaName(mediaJsonObj.getString("mediaName"));
                                    mediaBean.setNumber(mediaJsonObj.getString("number"));
                                    mediaBean.setId(mediaJsonObj.getString("itemId"));

                                    mediaBean.setOrderId(mediaJsonObj.getString("orderId"));

//                                    String positionXStr = mediaJsonObj.getString("positionX");
//                                    if (!TextUtils.isEmpty(positionXStr)) {
//                                        mediaBean.setPositionX(Double.parseDouble(positionXStr));
//                                    }
//
//                                    String positionYStr = mediaJsonObj.getString("positionY");
//                                    if (!TextUtils.isEmpty(positionYStr)) {
//                                        mediaBean.setPositionY(Double.parseDouble(positionYStr));
//                                    }
                                    mediaBean.setRegionId(mediaJsonObj.getString("regionId"));
                                    mediaBean.setRegionName(mediaJsonObj.getString("regionName"));

                                    String stateStr = mediaJsonObj.getString("state");
                                    if (!TextUtils.isEmpty(stateStr)) {
                                        if (stateStr.equals("WORKING")) {
                                            mediaBean.setState(CommonEnum.FeedBackState.WORKING);
                                        } else if (stateStr.equals("WAIT_CONFIRM")) {
                                            mediaBean.setState(CommonEnum.FeedBackState.WAIT_CONFIRM);
                                        } else if (stateStr.equals("COMPLETE")) {
                                            mediaBean.setState(CommonEnum.FeedBackState.COMPLETE);
                                        } else if (stateStr.equals("EXCEPTION")) {
                                            mediaBean.setState(CommonEnum.FeedBackState.EXCEPTION);
                                        } else if (stateStr.equals("PAUSE")) {
                                            mediaBean.setState(CommonEnum.FeedBackState.PAUSE);
                                        }

                                    }

                                    mediaBean.setAssignReceiveAccountId(mediaJsonObj.getString("assignReceiveAccountId"));
                                    mediaBean.setAssignReceiveAccountName(mediaJsonObj.getString("assignReceiveAccountName"));

                                    mediaBeanList.add(mediaBean);

                                }

                                patrolMediaListForMapListener.getPatrolMediaListForMapSuccessed(mediaBeanList);
                            }


                        } else {
                            String error_str = code;
                            error_str += "," + ConstantUtil.getErrorCode(code);
                            if (!json.isNull("error_str")) {
                                error_str += "," + json.getString("error_str");
                            }
                            patrolMediaListForMapListener.getPatrolMediaListForMapFailed(error_str);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {

                }
            }
        }).start();
    }

    public interface OnPatrolMediaListForMapListener {
        void getPatrolMediaListForMapSuccessed(List<MediaBean> mediaBeanList);

        void getPatrolMediaListForMapFailed(String desc);
    }
}
